<?php
class Place extends Vfriend_Db {	
	var $cache;
	var $clear;
	var $notify;
	var $behaviour;
	var $plogs;
	public function __construct() {
		$this->cache 		= new Vfriend_Cache();
		$this->clear 		= $this->cache->clear;
		$this->plogs 		= new Plogs();		
		$this->notify		= new Notification();		
		$this->behaviour	= new Behaviour();		
	}
	public function add($intUserId, $strName, $intCategoryId, $intSubCategoryId, $strCountry, $intProvince, $intDistrict, $strAddress, $strContent, $strMap, $strImage, &$intPlaceId=0) {
		try {				
			$this->prepare('call places_add(?,?,?,?,?,?,?,?,?,?)');
			$this->bindParam(1, $intUserId, 		PDO::PARAM_INT, 11);
			$this->bindParam(2, $strName, 			PDO::PARAM_STR, 200);
			$this->bindParam(3, $intCategoryId, 	PDO::PARAM_INT, 11);
			$this->bindParam(4, $intSubCategoryId, 	PDO::PARAM_INT, 11);
			$this->bindParam(5, $strCountry, 		PDO::PARAM_STR, 2);	
			$this->bindParam(6, $intProvince, 		PDO::PARAM_INT, 11);
			$this->bindParam(7, $intDistrict, 		PDO::PARAM_INT, 11);
			$this->bindParam(8, $strAddress, 		PDO::PARAM_STR, 150);			
			$this->bindParam(9, $strContent, 		PDO::PARAM_STR);				
			$this->bindParam(10, $strMap, 			PDO::PARAM_STR);				
			$arrResult 	= $this->fetchAll();			
			$intPlaceId  = isset($arrResult[0]['placeid']) ? $arrResult[0]['placeid'] : 0;
			$intError	= isset($arrResult[0]['errCode']) ? $arrResult[0]['errCode'] : 0;
			if($intPlaceId && $intError == 0) {					
				$this->addImage($intUserId, $intPlaceId, $strImage, '', 1);
				$this->clear->deletePlaces($intSubCategoryId, $intProvince);
				$objVfriendUser = new Vfriend_User();
				$arrRelation	= $objVfriendUser->getRelation($intUserId,2);
				if($arrRelation) {					
					foreach($arrRelation as $value) {						
						$this->logBehaviour($value,$intUserId,$intUserId,61,$intPlaceId);
					}
				}
				$this->logBehaviour($intUserId,$intUserId,$intUserId,61,$intPlaceId);
			}
			return $intError;
		} catch(Exception $e) {
			Vfriend_Function::logDb('Place::add - ' .$e->getMessage());			
			return null;
		}
	}
	public function update($intUserId, $intPlaceId, $strName, $intCategoryId, $intSubCategoryId, $strCountry, $intProvince, $intDistrict, $strAddress, $strContent, $strMap, $strImage) {
		try {			
			$this->prepare('call places_update(?,?,?,?,?,?,?,?,?,?,?)');
			$this->bindParam(1, $intUserId, PDO::PARAM_INT, 11);
			$this->bindParam(2, $intPlaceId, PDO::PARAM_INT, 11);
			$this->bindParam(3, $strName, PDO::PARAM_STR, 200);
			$this->bindParam(4, $intCategoryId, PDO::PARAM_INT, 11);
			$this->bindParam(5, $intSubCategoryId, PDO::PARAM_INT, 11);
			$this->bindParam(6, $strCountry, PDO::PARAM_STR, 2);
			$this->bindParam(7, $intProvince, PDO::PARAM_INT, 11);			
			$this->bindParam(8, $intDistrict, PDO::PARAM_INT, 11);			
			$this->bindParam(9, $strAddress, PDO::PARAM_STR, 150);			
			$this->bindParam(10, $strContent, PDO::PARAM_STR);
			$this->bindParam(11, $strMap, PDO::PARAM_STR);			
			$arrResult 	= $this->fetchAll();
			$intError  	= isset($arrResult[0]['errCode']) ? $arrResult[0]['errCode'] : 0;
			$intOldSubCateId = isset($arrResult[0]['subcategoryid']) ? $arrResult[0]['subcategoryid'] : 0;
			$intOldProvince = isset($arrResult[0]['province']) ? $arrResult[0]['province'] : 0;
			if($intError == 0) {
				if($strImage != ''){
					$this->addImage($intUserId, $intPlaceId, $strImage, '', 1);
				}
				$this->clear->deletePlaces($intOldSubCateId, $intOldProvince);				
				$this->clear->deletePlaces($intSubCategoryId, $intProvince);
				$this->clear->deletePlaceById($intPlaceId);
			}
			return $intError;
		} catch(Exception $e) {
			Vfriend_Function::logDb('Place::update - ' .$e->getMessage());			
			return null;
		}
	}	
	public function get($intPlaceId) {
		try {
			$strKey 	= PLACE_GET . $intPlaceId;
			$arrResult	= $this->cache->get($strKey);			
			if(!Vfriend_Function::isCached($arrResult)) {
				$this->prepare('call places_get(?)');
				$this->bindParam(1, $intPlaceId, PDO::PARAM_INT, 11);
				$arrResult = $this->fetchAll();
				$this->cache->set($strKey, $arrResult, PLACE_GET_EXPIRE);
			}			
			return isset($arrResult[0]) ? $arrResult[0] : null;
		} catch(Exeception $e) {
			Vfriend_Function::logDb('Place::get - ' .$e->getMessage());			
			return null;
		}
	}
	public function getAllForDaskboard() {
		try {
			$strKey 	= PLACE_GETALLFORDASKBOARD;
			$arrResult	= $this->cache->get($strKey);			
			if(!Vfriend_Function::isCached($arrResult)) {
				$this->prepare('call places_getall_dashboard()');
				$arrResult = $this->fetchAll();
				$this->cache->set($strKey, $arrResult, PLACE_GETALLFORDASKBOARD_EXPIRE);
			}			
			return isset($arrResult) ? $arrResult : null;
		} catch(Exeception $e) {
			Vfriend_Function::logDb('Place::get - ' .$e->getMessage());			
			return null;
		}
	}
	public function getRss() {
		try {
			$strKey 	= PLACE_GETRSS;
			$arrResult	= $this->cache->get($strKey);
			if(!Vfriend_Function::isCached($arrResult)) {
				$this->prepare('call places_getall_rss()');
				$arrResult = $this->fetchAll();
				$this->cache->set($strKey, $arrResult, PLACE_GETRSS_EXPIRE);
			}
			return isset($arrResult) ? $arrResult : null;
		} catch(Exeception $e) {
			Vfriend_Function::logDb('Place::getRss - ' .$e->getMessage());			
			return null;
		}
	}
	public function getAll($intSubcategoryId=0, $intId, $intProvince=0, $intFilter=0, $intOff, $intLimit, &$intTotal=0) {		
		try {				
			$arrResult	= null;
			$strKey		= '' ;
			if($intOff == 0 && $intId == 0) {
				$strKey		= PLACE_GETALL . $intSubcategoryId . $intProvince . $intFilter;
				$arrResult	= $this->cache->get($strKey);
			}
			if(!Vfriend_Function::isCached($arrResult)) {
				$this->prepare('call places_getall(?,?,?,?,?,?)');
				$this->bindParam(1, $intSubcategoryId, PDO::PARAM_INT, 11);	
				$this->bindParam(2, $intId, PDO::PARAM_INT, 11);			
				$this->bindParam(3, $intProvince, PDO::PARAM_INT, 11);
				$this->bindParam(4, $intFilter, PDO::PARAM_INT, 4);
				$this->bindParam(5, $intOff, PDO::PARAM_INT, 11);
				$this->bindParam(6, $intLimit, PDO::PARAM_INT, 4);		
				$arrResult 	= $this->fetchAll();
				if($strKey != '') {
					$this->cache->set($strKey, $arrResult, PLACE_GETALL_EXPIRE);
				}
			}
			
			$intTotal	= isset($arrResult[0]['foundRows']) ? $arrResult[0]['foundRows'] : 0;
			return isset($arrResult) ? $arrResult : null;
		} catch(Exception $e) {
			Vfriend_Function::logDb('Place::getAll - ' .$e->getMessage());			
			return null;
		}
	}
	public function getSmartSearch($strKey, $intProvince, $intDistrict, $intPlaceId=0) {
		try {
			$this->prepare('call places_getall_smartsearch(?,?,?,?)');
			$this->bindParam(1, $strKey, PDO::PARAM_STR, 255);
			$this->bindParam(2, $intProvince, PDO::PARAM_INT, 11);	
			$this->bindParam(3, $intDistrict, PDO::PARAM_INT, 11);	
			$this->bindParam(4, $intPlaceId, PDO::PARAM_INT, 11);	
			$arrResult 	= $this->fetchAll();
			return $arrResult;
		} catch(Exception $e) {
			Vfriend_Function::logDb('Place::getSmartSearch - ' .$e->getMessage());			
			return null;
		}
	}
	public function getSearch($strSubCateId, $intFilter, $strKeyword, $strProvince, $strDistrict, $strDateFrom, $strDateTo, $intOffset, $intLimit, &$intTotal) {
		try {
			if($intOffset < 0) $intOffset = 0;	
			$this->prepare('call places_search(?,?,?,?,?,?,?,?,?)');
			$this->bindParam(1, $intFilter, PDO::PARAM_INT, 11);	
			$this->bindParam(2, $strKeyword, PDO::PARAM_STR);	
			$this->bindParam(3, $strSubCateId, PDO::PARAM_STR);	
			$this->bindParam(4, $strProvince, PDO::PARAM_STR);	
			$this->bindParam(5, $strDistrict, PDO::PARAM_STR);	
			$this->bindParam(6, $strDateFrom, PDO::PARAM_STR);	
			$this->bindParam(7, $strDateTo, PDO::PARAM_STR);	
			$this->bindParam(8, $intOffset, PDO::PARAM_INT, 11);	
			$this->bindParam(9, $intLimit, PDO::PARAM_INT, 11);	
			$arrResult 	= $this->fetchAll();
				
			$intTotal = isset($arrResult[0]['foundRows']) ? $arrResult[0]['foundRows'] : 0;
			return isset($arrResult) ? $arrResult : null;
		} catch(Exception $e) {
			Vfriend_Function::logDb('Place::search - ' .$e->getMessage());			
			return null;
		}
	}
	public function getCmt($intUserId, $intPlaceId) {
		try {	
			$strKey	   = PLACE_GETCMT . $intUserId . $intPlaceId;
			$arrResult = $this->cache->get($strKey);
			if(!Vfriend_Function::isCached($arrResult)) {
				$this->prepare('call places_cmt_check(?,?)');
				$this->bindParam(1, $intUserId, PDO::PARAM_INT, 11);	
				$this->bindParam(2, $intPlaceId, PDO::PARAM_INT, 11);	
				$arrResult 	= $this->fetchAll();
				$this->cache->set($strKey, $arrResult, PLACE_GETCMT_EXPIRE);
			}
			$intTotal	= isset($arrResult[0]['pcmtid']) ? $arrResult[0]['pcmtid'] : 0;
			return $intTotal;
		} catch(Exception $e) {
			Vfriend_Function::logDb('Place::getCmt - ' .$e->getMessage());			
			return null;
		}
	}
	public function getTopCmt($intPlaceId,$intLimit=BEHAVIOUR_TOP_LIMIT) {
		try {	
			$strKey	 	= PLACE_GETTOPCMT . $intPlaceId;
			$arrResult	= $this->cache->get($strKey);
			if(!Vfriend_Function::isCached($arrResult)) {
				$this->prepare('call places_cmt_get_top(?,?)');
				$this->bindParam(1, $intPlaceId, PDO::PARAM_INT, 11);	
				$this->bindParam(2, $intLimit, PDO::PARAM_INT, 4);	
				$arrResult 	= $this->fetchAll();
				$this->cache->set($strKey, $arrResult, PLACE_GETTOPCMT_EXPIRE);
			}
			return isset($arrResult) ? $arrResult : null;
		} catch(Exception $e) {
			Vfriend_Function::logDb('Place::getTopCmt - ' .$e->getMessage());			
			return null;
		}
	}
	public function getCmtTotal($intPlaceId) {
		try {	
			$strKey	   = PLACE_GETCMTTOTAL . $intPlaceId;
			$arrResult = $this->cache->get($strKey);
			if(!Vfriend_Function::isCached($arrResult)) {
				$this->prepare('call places_cmt_get_total(?)');
				$this->bindParam(1, $intPlaceId, PDO::PARAM_INT, 11);	
				$arrResult 	= $this->fetchAll();
				$this->cache->set($strKey, $arrResult, PLACE_GETCMTTOTAL_EXPIRE);
			}
			$intTotal	= isset($arrResult[0]['foundRows']) ? $arrResult[0]['foundRows'] : 0;
			return $intTotal;
		} catch(Exception $e) {
			Vfriend_Function::logDb('Place::getCmtTotal - ' .$e->getMessage());			
			return null;
		}
	}
	public function getCmtList($intUserId, $intPlaceId, $intOffset, $intLimit, &$intHasComment=0) {
		try {		
			if($intOffset < 0) $intOffset = 0;
			$this->prepare('call places_cmt_getall(?,?,?,?)');
			$this->bindParam(1, $intUserId, PDO::PARAM_INT, 11);	
			$this->bindParam(2, $intPlaceId, PDO::PARAM_INT, 11);	
			$this->bindParam(3, $intOffset, PDO::PARAM_INT, 11);	
			$this->bindParam(4, $intLimit, PDO::PARAM_INT, 4);	
			$arrResult 	= $this->fetchAll();
			$intHasComment = isset($arrResult[0]['hasComment'])	? $arrResult[0]['hasComment'] : 0;
			return isset($arrResult) ? $arrResult : null;
		} catch(Exception $e) {
			Vfriend_Function::logDb('Place::getCmtList - ' .$e->getMessage());			
			return null;
		}
	}
	public function getCmtUser($intPlaceId) {
		try {	
			$strKey	 	= PLACE_GETCMTUSER . $intPlaceId;
			$arrResult 	= $this->cache->get($strKey);
			if(!Vfriend_Function::isCached($arrResult)) {
				$this->prepare('call places_cmt_get_user(?)');
				$this->bindParam(1, $intPlaceId, PDO::PARAM_INT, 11);	
				$arrResult 	= $this->fetchAll();
				$this->cache->set($strKey, $arrResult, PLACE_GETCMTUSER_EXPIRE);
			}
			return $arrResult;
		} catch(Exception $e) {
			Vfriend_Function::logDb('Place::getCmtUser - ' .$e->getMessage());			
			return null;
		}
	}
	public function addComment($intUserId, $intPlaceId, $strContent, $intRating) {
		try {
			$this->prepare('call places_cmt_add(?,?,?,?)');
			$this->bindParam(1, $intUserId, PDO::PARAM_INT, 11);
			$this->bindParam(2, $intPlaceId, PDO::PARAM_INT, 11);
			$this->bindParam(3, $strContent);
			$this->bindParam(4, $intRating, PDO::PARAM_INT, 4);
						
			$arrResult	= $this->fetchAll();
			$intError	= isset($arrResult[0]['errCode']) ? $arrResult[0]['errCode'] : 0;
			if($intError == 0) {
				$this->clear->deletePlaceById($intPlaceId);
				$arrInfo 	= $this->get($intPlaceId);
				$intOwnerId	= $arrInfo['userid'];
				// BEHAVIOUR
				$arrUser	= $this->getCmtUser($intPlaceId);
				if($arrUser) {										
					foreach($arrUser as $value) {
						$intReceiveId = $value['userid'];
						$this->logBehaviour($intReceiveId, $intOwnerId, $intUserId, 62, $intPlaceId);						
					}				
				}
				$this->logBehaviour($intOwnerId, $intOwnerId, $intUserId, 62, $intPlaceId);
				$this->logBehaviour($intUserId, $intOwnerId, $intUserId, 62, $intPlaceId);
				// LOG APP
				$intCommentId = isset($arrResult[0]['pcommentid']) ? $arrResult[0]['pcommentid'] : 0;
				$arrUserLike = $this->getLikeList($intPlaceId, -1, 0, $intTotal);								
				if($arrUserLike) {					
					$strLogsContent = Zend_Json::encode(array(
														'placeid'  	=> $intPlaceId,
														'title'  	=> $arrInfo['name'],
														'posterid' 	=> $intUserId,
														'content'	=> $strContent
													)
												);
					foreach($arrUserLike as $value) {
						$intULogId = $value['userid'];
						if($intULogId != $intUserId) {
							$this->plogs->insert($intULogId, 1, $strLogsContent, $intCommentId, $intPlaceId);
							$this->notify->insert($intULogId, 67, $strLogsContent, $intPlaceId);
						}
					}
				}				
			}
			return $intError;
		} catch(Exception $e) {
			Vfriend_Function::logDb('Place::addComment - ' .$e->getMessage());			
			return null;
		}
	}
	public function deleteFavourite($intUserId, $intPlaceId) {
		try {
			$this->prepare('call places_fav_delete(?,?)');
			$this->bindParam(1, $intUserId, PDO::PARAM_INT, 11);
			$this->bindParam(2, $intPlaceId, PDO::PARAM_INT, 11);
			$arrResult 	= $this->fetchAll();
			$intError	= isset($arrResult[0]['errCode']) ? $arrResult[0]['errCode'] : 0;
			if($intError == 0) {
				$this->clear->deletePlaceFavourite($intUserId);				
			}
			return $intError;
		} catch(Exception $e) {
			Vfriend_Function::logDb('Place::deleteFavourite - ' .$e->getMessage());			
			return null;
		}
	}	
	
	public function updateMap($intUserId, $intPlaceId, $strMap)
	{
		try {
			$this->prepare('call places_update_map(?,?,?)');
			$this->bindParam(1, $intUserId, PDO::PARAM_INT, 11);
			$this->bindParam(2, $intPlaceId, PDO::PARAM_INT, 11);
			$this->bindParam(3, $strMap, PDO::PARAM_STR);
			$arrResult 	= $this->fetchAll();
			$intError 	= isset($arrResult[0]['errCode']) ? $arrResult[0]['errCode'] : 0;
			if($intError == 0) 
			{
				$this->clear->deletePlaceById($intPlaceId);				
			}
			return $intError;
		} catch(Exception $e) {
			Vfriend_Function::logDb('Place::updateMap - ' . $e->getMessage());
			return null;
		}
	}
	
	public function addImage($intUserId, $intPlaceId, $strImage, $strNote, $intMain=0, $intLimit=PLACE_IMG_LIMIT)
	{
		try {
			$this->prepare('call places_img_add(?,?,?,?,?,?)');
			$this->bindParam(1, $intUserId, PDO::PARAM_INT, 11);
			$this->bindParam(2, $intPlaceId, PDO::PARAM_INT, 11);
			$this->bindParam(3, $strImage, PDO::PARAM_STR, 150);
			$this->bindParam(4, $strNote, PDO::PARAM_STR);
			$this->bindParam(5, $intMain, PDO::PARAM_INT, 1);
			$this->bindParam(6, $intLimit, PDO::PARAM_INT, 6);
			$arrResult 	= $this->fetchAll();
			$intError 	= isset($arrResult[0]['errCode']) ? $arrResult[0]['errCode'] : 0;
			if($intError == 0)
			{
				$strImage 	= isset($arrResult[0]['image']) ? $arrResult[0]['image'] : '';
				if($strImage != '') {
					$this->deleteImg($strImage);
				}
				$this->clear->deletePlaceById($intPlaceId);					
				$intPImageId = isset($arrResult[0]['pimageid']) ? $arrResult[0]['pimageid'] : 0;
				$arrUserLike = $this->getLikeList($intPlaceId, -1, 0, $intTotal);				
				if($arrUserLike) {
					$arrInfo		= $this->get($intPlaceId);
					$strLogsContent = Zend_Json::encode(array(
																'placeid'  	=> $intPlaceId,
																'title'  	=> $arrInfo['name'],
																'posterid' 	=> $intUserId
															)
														);
					foreach($arrUserLike as $value) {
						$intULogId = $value['userid'];
						if($intULogId != $intUserId) {
							$this->plogs->insert($intULogId, 2, $strLogsContent, $intPImageId, $intPlaceId);
							$this->notify->insert($intULogId, 68, $strLogsContent, $intPlaceId);
						}
					}	
				}
				
			}
			return $intError;
		} catch(Exception $e) {
			Vfriend_Function::logDb('Place::addImage - ' . $e->getMessage());
			return null;
		}
	}
	public function addLikeImage($intUserId, $intPlaceId, $intPimageId, &$intLike=0) {
		try {			
			$this->prepare('call places_limg_add(?,?,?)');
			$this->bindParam(1, $intUserId, PDO::PARAM_INT, 11);	
			$this->bindParam(2, $intPlaceId, PDO::PARAM_INT, 11);	
			$this->bindParam(3, $intPimageId, PDO::PARAM_INT, 11);	
			$arrResult 	= $this->fetchAll();	
			$intError	= isset($arrResult[0]['errCode']) ? $arrResult[0]['errCode'] : 0;
			if($intError == 0) {
				$intLike = isset($arrResult[0]['tlike']) ? $arrResult[0]['tlike'] : 0;
				$this->clear->deletePlaceForImg($intPimageId);
				$this->clear->deletePlaceById($intPlaceId);
			}
			return $intError;
		} catch(Exception $e) {			
			Vfriend_Function::logDb('Place::addLikeImage - ' .$e->getMessage());			
			return null;
		}
	}
	public function deleteImage($intUserId, $intPlaceId, $intPimageId) {
		try {			
			$this->prepare('call places_img_delete(?,?,?)');
			$this->bindParam(1, $intUserId, PDO::PARAM_INT, 11);	
			$this->bindParam(2, $intPlaceId, PDO::PARAM_INT, 11);	
			$this->bindParam(3, $intPimageId, PDO::PARAM_INT, 11);	
			$arrResult 	= $this->fetchAll();	
			$intError	= isset($arrResult[0]['errCode']) ? $arrResult[0]['errCode'] : 0;
			if($intError == 0) {				
				$this->clear->deletePlaceForImg($intPimageId);
				$this->clear->deletePlaceById($intPlaceId);
			}
			return $intError;
		} catch(Exception $e) {			
			Vfriend_Function::logDb('Place::deleteImage - ' .$e->getMessage());			
			return null;
		}
	}
	public function getImage($intPimageId)
	{
		try {
			$strKey 	= PLACE_GETIMAGE . $intPimageId;
			$arrResult	= $this->cache->get($strKey);
			if(!Vfriend_Function::isCached($arrResult) || 1) {
				$this->prepare('call places_img_get(?)');
				$this->bindParam(1, $intPimageId, PDO::PARAM_INT, 11);	
				$arrResult 	= $this->fetchAll();	
				$this->cache->set($strKey, $arrResult,PLACE_GETIMAGE_EXPIRE);
			}			
			return isset($arrResult[0]) ? $arrResult[0] : null;
		} catch(Exception $e) {			
			Vfriend_Function::logDb('Place::getImage - ' .$e->getMessage());			
			return null;
		}
	}	
	public function getImageTotal($intPlaceId)
	{
		try {				
			$strKey 	= PLACE_GETIMAGETOTAL . $intPlaceId;
			$arrResult	= $this->cache->get($strKey);
			if(!Vfriend_Function::isCached($arrResult)) {
				$this->prepare('call places_img_get_total(?)');
				$this->bindParam(1, $intPlaceId, PDO::PARAM_INT, 11);	
				$arrResult 	= $this->fetchAll();			
				$this->cache->set($strKey,$arrResult,PLACE_GETIMAGETOTAL_EXPIRE);
			}			
			return isset($arrResult[0]['foundRows']) ? $arrResult[0]['foundRows'] : 0;
		} catch(Exception $e) {
			Vfriend_Function::logDb('Place::getImageTotal - ' .$e->getMessage());			
			return null;
		}
	}	
	
	public function getImages($intPlaceId, $intOffset, $intLimit, &$intTotal=0)
	{
		try {		
			$arrResult	= null;
			if($intOffset < 0) $intOffset = 0;
			if($intOffset == 0) {
				$strKey 	= PLACE_GETIMAGES . $intPlaceId . '0';
				$arrResult	= $this->cache->get($strKey);
			}
			if(!Vfriend_Function::isCached($arrResult)) {
				$this->prepare('call places_img_getall(?,?,?,?)');
				$this->bindParam(1, $intType=0, PDO::PARAM_INT, 11);	
				$this->bindParam(2, $intPlaceId, PDO::PARAM_INT, 11);	
				$this->bindParam(3, $intOffset, PDO::PARAM_INT, 11);	
				$this->bindParam(4, $intLimit, PDO::PARAM_INT, 4);	
				$arrResult 	= $this->fetchAll();	
				if($intOffset == 0) {
					$this->cache->set($strKey,$arrResult,PLACE_GETIMAGES_EXPIRE);
				}
			}
			$intTotal = isset($arrResult[0]['foundRows']) ? $arrResult[0]['foundRows'] : 0;			
			return isset($arrResult) ? $arrResult : null;
		} catch(Exception $e) {
			Vfriend_Function::logDb('Place::getImages - ' .$e->getMessage());			
			return null;
		}
	}	
	public function updatePromotion($intUserId, $intPlaceId, $strPromotion)
	{
		try {
			$this->prepare('call places_update_promotion(?,?,?)');
			$this->bindParam(1, $intUserId, PDO::PARAM_INT, 11);
			$this->bindParam(2, $intPlaceId, PDO::PARAM_INT, 11);
			$this->bindParam(3, $strPromotion, PDO::PARAM_STR);
			$arrResult 	= $this->fetchAll();
			$intError 	= isset($arrResult[0]['errCode']) ? $arrResult[0]['errCode'] : 0;
			if($intError == 0) 
			{
				$this->clear->deletePlaceById($intPlaceId);
			}
			return $intError;
		} catch(Exception $e) {
			Vfriend_Function::logDb('Place::updatePromotion - ' . $e->getMessage());
			return null;
		}
	}
	
	public function updateContact($intUserId, $intPlaceId, $strContact)
	{
		try {
			$this->prepare('call places_update_contact(?,?,?)');
			$this->bindParam(1, $intUserId, PDO::PARAM_INT, 11);
			$this->bindParam(2, $intPlaceId, PDO::PARAM_INT, 11);
			$this->bindParam(3, $strContact, PDO::PARAM_STR);
			$arrResult 	= $this->fetchAll();
			$intError 	= isset($arrResult[0]['errCode']) ? $arrResult[0]['errCode'] : 0;
			if($intError == 0) 
			{				
				$this->clear->deletePlaceById($intPlaceId);				
			}
			return $intError;
		} catch(Exception $e) {
			Vfriend_Function::logDb('Place::updateContact - ' . $e->getMessage());
			return null;
		}
	}
	
	public function getFavourList($intUserId, $intOffset, $intLimit, &$intTotal) {
		try {
			if($intOffset < 0) $intOffset = 0;
			$arrResult	= null;
			if($intOffset == 0) {
				$strKey	   	= PLACE_GETALLFAVOURITE . $intUserId;
				$arrResult 	= $this->cache->get($strKey);
			}
			if(!Vfriend_Function::isCached($arrResult)) {	
				$this->prepare('call places_fav_getall(?,?,?)');
				$this->bindParam(1, $intUserId, PDO::PARAM_INT, 11);	
				$this->bindParam(2, $intOffset, PDO::PARAM_INT, 11);	
				$this->bindParam(3, $intLimit, PDO::PARAM_INT, 4);	
				$arrResult 	= $this->fetchAll();
				if($intOffset == 0)
					$this->cache->set($strKey, $arrResult, PLACE_GETALLFAVOURITE_EXPIRE);
			}
			$intTotal = isset($arrResult[0]['foundRows']) ? $arrResult[0]['foundRows'] : 0;
			return isset($arrResult) ? $arrResult : null;
		} catch(Exception $e) {
			Vfriend_Function::logDb('Place::getFavourList - ' .$e->getMessage());			
			return null;
		}
	}
	
	public function getLikeList($intPlaceId, $intOffset, $intLimit, &$intTotal) {
		try {
			if($intOffset < 0) $intOffset = 0;
			$arrResult = null;
			if($intOffset == 0) {
				$strKey	   = PLACE_GETALLMEMBERLIKE . $intPlaceId;
				$arrResult = $this->cache->get($strKey);
			}
			if(!Vfriend_Function::isCached($arrResult)) {	
				$this->prepare('call places_like_getall(?,?,?)');
				$this->bindParam(1, $intPlaceId, PDO::PARAM_INT, 11);	
				$this->bindParam(2, $intOffset, PDO::PARAM_INT, 11);	
				$this->bindParam(3, $intLimit, PDO::PARAM_INT, 4);	
				$arrResult 	= $this->fetchAll();
				if($intOffset == 0) {
					$this->cache->set($strKey, $arrResult, PLACE_GETALLMEMBERLIKE_EXPIRE);
				}
			}			
			$intTotal = isset($arrResult[0]['foundRows']) ? $arrResult[0]['foundRows'] : 0;
			return isset($arrResult) ? $arrResult : null;
		} catch(Exception $e) {
			Vfriend_Function::logDb('Place::getLikeList - ' .$e->getMessage());			
			return null;
		}
	}
	public function getLike($intPlaceId, $intLimit=BEHAVIOUR_TOP_LIMIT) {
		try {
			$strKey	 	= PLACE_GETLIKE . $intPlaceId;
			$arrResult	= $this->cache->get($strKey);			
			if(!Vfriend_Function::isCached($arrResult)) {
				$this->prepare('call nlikes_get_top(?,?)');
				$this->bindParam(1, $intPlaceId, PDO::PARAM_INT, 11);	
				$this->bindParam(2, $intLimit, PDO::PARAM_INT, 4);	
				$arrResult 	= $this->fetchAll();
				$this->cache->set($strKey, $arrResult, PLACE_GETLIKE_EXPIRE);
			}
			return isset($arrResult) ? $arrResult : null;
		} catch(Exception $e) {
			Vfriend_Function::logDb('Place::getLike - ' . $e->getMessage());
			return null;
		}
	}
	public function addLike($intUserId, $intPlaceId, &$intLike=0)
	{
		try {
			$this->prepare('call places_like_add(?,?)');
			$this->bindParam(1, $intUserId, PDO::PARAM_INT, 11);
			$this->bindParam(2, $intPlaceId, PDO::PARAM_INT, 11);			
			$arrResult 	= $this->fetchAll();
			$intError 	= isset($arrResult[0]['errCode']) ? $arrResult[0]['errCode'] : 0;
			if($intError == 0) {					
				$this->clear->deletePlaceById($intPlaceId);	
				$this->clear->deletePlaceFavourite($intUserId);	
				$arrInfo	= $this->get($intPlaceId);
				$intLike	= $arrInfo['tlike'];
			}
			return $intError;
		} catch(Exception $e) {
			Vfriend_Function::logDb('Place::addLike - ' . $e->getMessage());
			return null;
		}
	}
	public function getQuote($intPlaceId, $intType) {
		try {
			$strKey	 	= PLACE_GETQUOTE . $intPlaceId . $intType;
			$arrResult	= $this->cache->get($strKey);
			if(!Vfriend_Function::isCached($arrResult)) {
				$this->prepare('call news_get_quote(?,?)');
				$this->bindParam(1, $intPlaceId, PDO::PARAM_INT, 11);	
				$this->bindParam(2, $intType, PDO::PARAM_INT, 4);	
				$arrResult 	= $this->fetchAll();
				$this->cache->set($strKey, $arrResult, PLACE_GETQUOTE_EXPIRE);
			}
			return (isset($arrResult[0]['pcmtid']) && $arrResult[0]['pcmtid'] > 0)  ? $arrResult[0] : null;
		} catch(Exception $e) {
			Vfriend_Function::logDb('News::getQuote - ' . $e->getMessage());
			return null;
		}
	}
	public function logBehaviour($intUserId,$intOwnerId,$intPosterId,$intType,$intSrcId) {
		$strQueryCore 		= 'get';
		$strQueryLike		= 'getLike';
		$strQueryTotalCmt	= 'getCmtTotal';
		$strQueryCmt		= 'getTopCmt';		
		$strQueryQuote		= 'getQuote';
		$this->behaviour->insert($intUserId,
								 $intOwnerId,
								 $intPosterId,
								 $strQueryCore,
								 $strQueryLike,
								 $strQueryTotalCmt,
								 $strQueryCmt,
								 $strQueryQuote,
								 $intType,
								 $intSrcId);
	}
	public function deleteImg($strImg) {		
		if(file_exists(DIR_PLACE_FULL . $strImg))
			@unlink(DIR_PLACE_FULL . $strImg);
		if(file_exists(DIR_PLACE_THUMB . $strImg))
			@unlink(DIR_PLACE_THUMB . $strImg);
		if(file_exists(DIR_PLACE_SMALL . $strImg))
			@unlink(DIR_PLACE_SMALL . $strImg);
	}
}
?>
